package com.ymq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymq.reggie.dto.DishDto;
import com.ymq.reggie.entity.Category;
import com.ymq.reggie.entity.Dish;
import com.ymq.reggie.entity.DishFlavor;
import com.ymq.reggie.mapper.DishMapping;
import com.ymq.reggie.service.CategoryService;
import com.ymq.reggie.service.DishFlavorService;
import com.ymq.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapping, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Transactional
    public void saveWithflave(DishDto dishDto) {
        this.save(dishDto);
        Long id = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors =flavors.stream().map((item)->{
           item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
       dishFlavorService.saveBatch(flavors);
        }

    /**
     * 根据id查询菜品和口味
     * @param id
     * @return
     */
    @Override
    public DishDto getById(Long id) {
       // Dish dish1 = this.getById(id);
        Dish dish = super.getById(id);

        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
       //查询当前口味信息
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        //数据拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
//        list.stream().map((item)->{
//
//        })
        dishDto.setFlavors(list);


        return dishDto;
    }

    /**
     * 修改菜品
     * @param dishDto
     */
    @Transactional
    public void updateDishWithFlavor(DishDto dishDto) {
      this.updateById(dishDto);
      //删除口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);
        //添加口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
       flavors= flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
       dishFlavorService.saveBatch(flavors);



    }

    @Override
    public List<DishDto> updateRedisDish(Dish dish) {
        Long categoryId = dish.getCategoryId();
        String dishKey="dish_"+categoryId+"_"+dish.getStatus();
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lambdaQueryWrapper);

         List<DishDto> list1 = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId1 = item.getCategoryId();
            Category category = categoryService.getById(categoryId1);
            String name1 = category.getName();
            dishDto.setCategoryName(name1);
            //口味信息
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list2 = dishFlavorService.list(lambdaQueryWrapper1);
            dishDto.setFlavors(list2);

            return dishDto;
        }).collect(Collectors.toList());
          redisTemplate.opsForValue().set(dishKey,list1,60, TimeUnit.MINUTES);
          return  list1;

    }


}

